import jsonpath
import  allure
from utils.send_request import send_jdbc_request
import logging

@allure.step("3.HTTP断言")
def http_assert(case,response,index=0):
    expected = eval(case['expected'])
    if case["check"] :
        check = eval(case["check"])
        for c, e in zip(check,expected):
            result = jsonpath.jsonpath(response.json(), f'$..{c}')[index]
            logging.info(f"3.HTTP响应断言内容: 实际结果({result}) == 预期结果({e})")
            assert result == e

    else:
        for e in expected:
            logging.info(f"3.HTTP响应断言内容: 预期结果({e}) in 实际结果({response.text})")
            assert str(e) in response.text


def jdbc_assert(case):
    if case["sql_check"] and case["sql_expected"]:
        with allure.step("3.JDBC断言"):
            result = send_jdbc_request(case["sql_check"])
            logging.info(f"3.JDBC响应断言内容: 实际结果({result}){type(result)} == 预期结果({case['sql_expected']}){type(case['sql_expected'])}")
            assert result == case["sql_expected"]